package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.model.CaseTypeModel;
import com.wx.model.PageModel;
import com.wx.model.ProductModel;
import com.wx.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/back/product")
public class productController {
    @Autowired
    IProductService iProductService;

    @RequestMapping("/pageAndFindAll")
    public JSONObject pageAndFindAll(@RequestBody PageModel pageModel){
        pageModel.setPageNum(pageModel.getPageNum()==null || "".equals(pageModel.getPageNum())? 1 :(pageModel.getPageNum()));
        pageModel.setPageSize(pageModel.getPageSize()==null || "".equals(pageModel.getPageSize()) ? 10:(pageModel.getPageSize()));
        List<ProductModel> list= iProductService.pageAndFindAll(pageModel);
        int number=iProductService.getCont(pageModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }

    @RequestMapping("/conditionFindAll")
    public JSONObject conditionFindAll(@RequestBody ProductModel productModel){
        List<ProductModel> list=iProductService.conditionFindAll(productModel);
        int number=iProductService.getConditionFindAllCont(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }

    @RequestMapping("/productAdd")
    public JSONObject productAdd(@RequestBody ProductModel productModel){
        int count=iProductService.productAdd(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }

    @RequestMapping("/casetypeFindAll")
    public JSONObject casetypeFindAll(){
        List<CaseTypeModel> list=iProductService.casetypeFindAll();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @RequestMapping("/findById")
    public JSONObject findById(@RequestBody ProductModel productModel, HttpSession session){
       ProductModel productModel1= iProductService.findById(productModel);
       String imgHref=productModel1.getImgHref();
       session.setAttribute("logo",imgHref);
       JSONObject jsonObject=new JSONObject();
       jsonObject.put("productModel",productModel1);
       return jsonObject;
    }

    @RequestMapping("/productUpdate")
    public JSONObject productUpdate(@RequestBody ProductModel productModel){
        int count=iProductService.productUpdate(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }

    @RequestMapping("/producatDeleteById")
    public JSONObject producatDeleteById(@RequestBody ProductModel productModel){
        int count=iProductService.producatDeleteById(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }
}
